package pers.crayon.baseprogram.task;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ScheduledFuture;

/**
 * @author wangzg13
 * @since 2022/10/16
 */
public class ThreadPoolTaskSchedulerTest {

    private static SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    private static Map<String, String> threadMap = new HashMap<>();
    private static List<String> threadList = new ArrayList<>();


    public static void main(String[] args) {
        //String cron = "0 0 0 * * ?";
        String cron = "*/5 * * * * ?";
        System.out.println(sDateFormat.format(new Date()).toString());
        //for (int i = 0; i < 6; i++) {
        ScheduledFuture scheduledFuture = executeThreadTask(cron);
        ScheduledFuture scheduledFuture2 = executeThreadTask2(cron);
        System.out.println(scheduledFuture.hashCode());
        System.out.println(scheduledFuture2.hashCode());
        try {
            Thread.sleep(11000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(scheduledFuture2.isDone());
        scheduledFuture2.cancel(true);
        System.out.println(scheduledFuture2.isDone());
    }

    /**
     * 执行定时线程池任务
     */
    public static ScheduledFuture executeThreadTask(String cron) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = TaskExecutors.getThreadPoolTaskScheduler();
        ScheduledFuture<?> schedule = threadPoolTaskScheduler.schedule(new TestThread(), new CronTrigger(cron));
        return schedule;
    }

    /**
     * 执行定时线程池任务
     * 可以使用cancel取消定时任务,使用 isDone查看定时任务是否还在执行
     */
    public static ScheduledFuture executeThreadTask2(String cron) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = TaskExecutors.getThreadPoolTaskScheduler();
        ScheduledFuture<?> schedule = threadPoolTaskScheduler.schedule(new TestThread2(), new CronTrigger(cron));
        return schedule;
    }

    /**
     * 一个测试线程
     */
    private static class TestThread implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(sDateFormat.format(new Date()).toString() + ":" + Thread.currentThread().getName() + "当前线程池大小:" + TaskExecutors.getThreadPoolTaskScheduler().getPoolSize() + ",当前活跃线程大小:" + TaskExecutors.getThreadPoolTaskScheduler().getActiveCount());
        }
    }

    /**
     * 第二个线程
     */
    private static class TestThread2 implements Runnable {
        @Override
        public void run() {
            System.out.println(sDateFormat.format(new Date()).toString() + "=========:" + Thread.currentThread().getName() + "当前线程池大小:" + TaskExecutors.getThreadPoolTaskScheduler().getPoolSize() + ",当前活跃线程大小:" + TaskExecutors.getThreadPoolTaskScheduler().getActiveCount());
        }
    }
}
